package com.fanxuankai.boot.mqbroker.producer.autoconfigure;

import com.fanxuankai.boot.mqbroker.autoconfigure.Compensation;
import com.fanxuankai.boot.mqbroker.autoconfigure.MqBrokerProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

/**
 * @author fanxuankai
 */
@ConfigurationProperties(prefix = MqBrokerProducerProperties.PREFIX)
public class MqBrokerProducerProperties {
    public static final String PREFIX = MqBrokerProperties.PREFIX + ".producer";
    public static final String ENABLED = PREFIX + ".enabled";
    /**
     * 是否开启生产者
     */
    private boolean enabled = true;
    /**
     * 最大重试次数
     */
    private int maxRetry = 3;
    /**
     * 回调超时 ms
     */
    private long callbackTimeout = 300_000;
    /**
     * 异步确认，开启后，成功发送消息后异步标记消息为成功
     */
    private boolean asyncConfirm;
    /**
     * 延迟发送
     */
    private Delayed delayed = new Delayed();
    /**
     * 补偿
     */
    @NestedConfigurationProperty
    private Compensation compensation = new Compensation();
    /**
     * 开启延迟消息(比如: RabbitMQ delayed)
     */
    private boolean enabledDelayedMessage;
    /**
     * 消息重试配置，例如：消息丢失后重发
     */
    private Retry retry = new Retry();

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public int getMaxRetry() {
        return maxRetry;
    }

    public void setMaxRetry(int maxRetry) {
        this.maxRetry = maxRetry;
    }

    public long getCallbackTimeout() {
        return callbackTimeout;
    }

    public void setCallbackTimeout(long callbackTimeout) {
        this.callbackTimeout = callbackTimeout;
    }

    public boolean isAsyncConfirm() {
        return asyncConfirm;
    }

    public void setAsyncConfirm(boolean asyncConfirm) {
        this.asyncConfirm = asyncConfirm;
    }

    public Delayed getDelayed() {
        return delayed;
    }

    public void setDelayed(Delayed delayed) {
        this.delayed = delayed;
    }

    public Compensation getCompensation() {
        return compensation;
    }

    public void setCompensation(Compensation compensation) {
        this.compensation = compensation;
    }

    public boolean isEnabledDelayedMessage() {
        return enabledDelayedMessage;
    }

    public void setEnabledDelayedMessage(boolean enabledDelayedMessage) {
        this.enabledDelayedMessage = enabledDelayedMessage;
    }

    public Retry getRetry() {
        return retry;
    }

    public void setRetry(Retry retry) {
        this.retry = retry;
    }

    public static class Delayed {
        /**
         * 是否开启
         */
        private boolean enabled = true;
        /**
         * 拉取数据的间隔 ms
         */
        private long interval = 5_000;

        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }

        public long getInterval() {
            return interval;
        }

        public void setInterval(long interval) {
            this.interval = interval;
        }
    }

    public static class Retry {
        /**
         * 是否开启
         */
        private boolean enabled = true;
        /**
         * 超时 ms
         */
        private long timeout = 1_800_000;
        /**
         * 限制数量
         */
        private int limit = 100;

        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }

        public long getTimeout() {
            return timeout;
        }

        public void setTimeout(long timeout) {
            this.timeout = timeout;
        }

        public int getLimit() {
            return limit;
        }

        public void setLimit(int limit) {
            this.limit = limit;
        }
    }

    public static class ExternalEvent {
        /**
         * 事件名
         */
        private String name;
        /**
         * 目标数据库实例名，不能跨数据库
         */
        private String schema;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getSchema() {
            return schema;
        }

        public void setSchema(String schema) {
            this.schema = schema;
        }
    }
}
